package com.sharemarking.application.wechat.service;

import com.alibaba.fastjson.JSONObject;
import com.sharemarking.application.common.api.ResponseParams;
import com.sharemarking.application.common.utils.HttpUtil;
import com.sharemarking.application.wechat.entity.Client;
import com.sharemarking.application.wechat.repository.ClientRepo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/16.
 */
@Service
@Transactional
public class ClientService {

    @Value("${wmp.appid}")
    private String appId;

    @Value("${wmp.secret}")
    private String secret;

    @Autowired
    ClientRepo clientRepo;

    /**
     * 登录验证
     *
     * @return
     */
    public ResponseParams login(HttpServletRequest request) {
        ResponseParams responseParams = new ResponseParams();
        String nickName;
        Integer sex;
        String avatar;
        String address;
        String code;
        String openId = null;

        try {
            nickName = StringUtils.isEmpty(request.getParameter("nickName")) ? null
                    : request.getParameter("nickName");
            sex = StringUtils.isEmpty(request.getParameter("sex")) ? null
                    : Integer.parseInt(request.getParameter("sex"));
            avatar = StringUtils.isEmpty(request.getParameter("avatar")) ? null
                    : request.getParameter("avatar");
            address = StringUtils.isEmpty(request.getParameter("address")) ? null
                    : request.getParameter("address");
            code = StringUtils.isEmpty(request.getParameter("code")) ? null
                    : request.getParameter("code");
        } catch (Exception e) {
            responseParams.isRequiredParamsError();
            return responseParams;
        }

        //获取openId
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
        String json = HttpUtil.get(url);
        Map<String, Object> map = JSONObject.parseObject(json);
        openId = map.get("openid") == null ? null : map.get("openid")
                .toString();
        if (openId == null) {
            responseParams.isDataError("errcode:" + map.get("errcode")
                    + " errmsg:" + map.get("errmsg"));
            return responseParams;
        }

        //检测数据库是否有该客户
        Client client = clientRepo.findByOpenIdAndDelFlag(openId, false);
        if (client == null){
            Client c = new Client();
            c.setNickName(nickName);
            c.setSex(sex);
            c.setAvatar(avatar);
            c.setAddress(address);
            c.setOpenId(openId);
            clientRepo.save(c);
        }

        responseParams.getData().put("id", client.getId());
        responseParams.getData().put("openId", openId);
        responseParams.isSuccess();
        return responseParams;
    }
}
